<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单号" prop="yfCode">
        <el-input
          v-model="queryParams.yfCode"
          placeholder="请输入应付款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="yfpayList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="付款单号" align="center" prop="yfCode" />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column label="总数量" align="center" prop="zsum" />
      <el-table-column label="总金额" align="center" prop="zprice" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-popover
            ref="popoverRef"
            placement="right"
            width="300"
            trigger="click"
          >
            <el-form ref="form" :model="form" :rules="rules" label-width="68px">
              <el-form-item label="发票号" label-width="68px" prop="invoice">
                <el-input v-model="form.invoice" placeholder="请输入发票号" />
              </el-form-item>
              <el-button type="primary" size="mini" @click="hxPay(scope.row)"
              v-hasPermi="['supermarket:yfpay:hxPay']"
                >确 定</el-button
              >
            </el-form>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              slot="reference"
              v-hasPermi="['supermarket:yfpay:hx']"
              >核销</el-button
            >
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listYfpay, hxCjPay } from "@/api/supermarket/yfpay";
export default {
  name: "Yfpay",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应付汇总表格数据
      yfpayList: [],
      // 弹出层标题
      title: "",
      yfpayId: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplyId: null,
        yfCode: null,
        zprice: null,
        createName: null,
        isSh: 0,
        isHx: 0,
        shName: null,
        shTime: null,
        payTime: null,
        invoice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        invoice: [
          { required: true, message: "发票号不能为空", trigger: "blur" },
          {
            pattern: /^[0-9]{8,}$/,
            message: "发票号必须为至少8位数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应付汇总列表 */
    getList() {
      this.loading = true;
      listYfpay(this.queryParams).then((response) => {
        this.yfpayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    hxPay(row) {
      this.$refs.form.validate((valid) => {
        if (!valid) return false;
        this.form.yfpayId = row.yfpayId;
        hxCjPay(this.form).then((res) => {
          this.$modal.msgSuccess("核销成功！");
          this.form.invoice = null;
          this.getList();
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        yfpayId: null,
        supplyId: null,
        zprice: null,
        createTime: null,
        createName: null,
        isSh: null,
        shName: null,
        shTime: null,
        payTime: null,
        invoice: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
